//
//  GYUserInfo.m
//  GoodLuck
//
//  Created by apple on 2018/3/2.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYUserInfo.h"
#import "GYNetWorkTools.h"
#import "GYUser.h"

@implementation GYUserInfo

+ (void)loadUserInfo:(void (^)(GYUserInfo * _Nullable, NSError * _Nullable))finish{
    //1.获取登入key
    GYUser *user = [GYUser loadUserInfo];
    
    //2.加载
    [EasyLodingView showLodingText:@"正在加载..." config:^EasyLodingConfig *{
        EasyLodingConfig *config = [EasyLodingConfig shared];
        config.showOnWindow = YES;
        config.superReceiveEvent = NO;
        
        return config;
    }];
    [GYNetWorkTools requestUserInfo:user.key success:^(NSDictionary *obj) {
        //NSLog(@"obj=======%@",obj);
        int code = [[obj objectForKey:@"code"] intValue];
        NSDictionary *datas = [obj objectForKey:@"datas"];
        if (code == 200) {
            [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
            NSDictionary *member_info = [datas objectForKey:@"member_info"];
            GYUserInfo *userInfo = [[GYUserInfo alloc] initWithDic:member_info];
            finish(userInfo,nil);
        }else if (code == 400){
            [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
            [EasyTextView showText:@"加载数据出错,请重新加载!"];
            NSLog(@"网络加载个人信息错误====%@",[datas objectForKey:@"error"]);
        }
    } error:^(NSError *error) {
        [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
        [EasyTextView showText:@"加载数据出错,请重新加载!"];
        finish(nil,error);
    }];
}

- (void)setAvator:(NSString *)avator {
    _avator = avator;
    
    //转换成URL
    self.avatorURL = [NSURL URLWithString:avator];
}

- (instancetype)initWithDic:(NSDictionary *)dic {
    if ([super init]) {
        [self setValuesForKeysWithDictionary:dic];
    }
    return  self;
}

- (void)setValue:(id)value forKey:(NSString *)key {
    [super setValue:value forKey:key];
    if ([key isEqualToString:@"return"]) {
        self.returnCount = [value intValue];
        return;
    }
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    
}

- (NSString *)description {
    NSArray *keys = @[@"level_name",@"member_name",@"avator",@"favorites_store",@"favorites_goods"];
    return [NSString stringWithFormat:@"%@",[self dictionaryWithValuesForKeys:keys]];
}
@end
